/** \file log.h
 * @brief 日志控制输出头文件
 * @author 陈高报
 * @date  2012/3/15
 * @version 1.0
 */
#ifndef LOG_H
#define LOG_H
#include <assert.h>
#include <pthread.h>
#include <stdio.h>

#define LOG_STR_MAX_LEN 1024

/** \class CDebugLog log.h
 * \brief 日志类
 */
typedef enum _DEBUGLEVEL
{
    EDEBUG_LEVEL_ERROR = 1,
    EDEBUG_LEVEL_WARN,
    EDEBUG_LEVEL_MESSAGE,
    EDEBUG_LEVEL_NORMAL,
}DEBUGLEVEL;

#define DISALLOW_COPY_AND_ASSIGN(CDebugLog); \
        CDebugLog(const CDebugLog &);   \
        CDebugLog &operator =(const CDebugLog &)

class CDebugLog
{
public:
        static CDebugLog * Instance();

protected:
    /**
    * contructor
    * @param level 日志级别，级别越高打印的信息越多
    */
    explicit CDebugLog(DEBUGLEVEL level=EDEBUG_LEVEL_ERROR);  //默认只打印错误日志

public:
    ~CDebugLog();

    /**
    * 日志输出函数
    * @param level 此次打印的日志级别，级别越高打印的信息越多
    * @param arg 字符串
    */
    void DebugLog(const char *arg, ...);
    void DebugLog(DEBUGLEVEL level, const char *arg, ...);
    DEBUGLEVEL SetDebugLog(DEBUGLEVEL level)  {assert(level>=1);debugLevel = level; return debugLevel;}
    DEBUGLEVEL GetDebugLevel()   {return debugLevel;}
    int SetLogFileName(const char *);

private:
    DEBUGLEVEL debugLevel;
    static CDebugLog *pDebugLog;
    FILE *fp;
    char *filename;
    pthread_mutex_t m_Mutex;
    char time_buf[LOG_STR_MAX_LEN];
    char str_buf[LOG_STR_MAX_LEN];
    DISALLOW_COPY_AND_ASSIGN(CDebugLog);
};

#endif // LOG_H
